package com.wanxi.service.front.impl;

import com.wanxi.dao.front.NewsDao;
import com.wanxi.domain.ResultModel;
import com.wanxi.domain.front.NewsModel;
import com.wanxi.service.front.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;

    @Override
    public int getCount() {
        int count = newsDao.getCount();

        /*ResultModel resultModel = new ResultModel();
        resultModel.setData(count);*/
        return count;
    }

    //查询热点新闻，置顶新闻，普通新闻
    @Override
    public List<NewsModel> findAll(int page, int limit) {
        int page1  = (page-1)*2;
        List<NewsModel> hotNewsModels = newsDao.findHotNews(page1,2);
        List<NewsModel> topNewsModels = newsDao.findTopNews(page1,2);
        limit = limit-hotNewsModels.size()-hotNewsModels.size();
        page  = (page-1)*limit;
        List<NewsModel> normalNewsModels = newsDao.findNormalNews(page,limit);

        List<NewsModel> all = new ArrayList<>();
        all.addAll(hotNewsModels);
        all.addAll(topNewsModels);
        all.addAll(normalNewsModels);

        /*ResultModel resultModel = new ResultModel();
        resultModel.setData(all);*/
        return all;
    }

    //获取某一类型的新闻数量
    @Override
    public int getTypeCount(String typeName) {
        int count = newsDao.getTypeCount(typeName);

        /*ResultModel resultModel = new ResultModel();
        resultModel.setData(count);*/
        return count;
    }

    //查询某一类型（热点，置顶，普通）的新闻信息
    @Override
    public List<NewsModel>  findByType(int page, int limit, String typeName) {

        int page1  = (page-1)*2;
        List<NewsModel> hotNews = newsDao.findHotNewsByType(page1,limit,typeName);
        List<NewsModel> topNews = newsDao.findTopNewsByType(page1,limit,typeName);

        limit = limit-hotNews.size()-topNews.size();
        page  = (page-1)*limit;
        List<NewsModel> normalNews= newsDao.findNormalNewsByType(page,limit,typeName);

        List<NewsModel> all = new ArrayList<>();
        all.addAll(hotNews);
        all.addAll(topNews);
        all.addAll(normalNews);

        /*ResultModel resultModel = new ResultModel();
        resultModel.setData(all);*/

        return all;
    }
}
